//纵向选择器初始化
flyList = {};
flyList.init = function(){    
    var date= getStorage("dateTime");
	var dateArr=date.split("-");
    flyList.setDate(dateArr[0],dateArr[1],dateArr[2]);
	  $("#date").removeClass("uhide");
//    flyList.setDate(iG.getYear(),iG.getMonth(),iG.getDay());
      $("#date>div").on("click",flyList.selectDate);    
};

//设置日期
flyList.setDate = function(year,month,day){
    var today = new Date(iG.getYear(),iG.getMonth()-1,iG.getDay());
    var yesterday = new Date(iG.getYear(),iG.getMonth()-1,iG.getDay());
    yesterday.setDate(yesterday.getDate()-1);
    var tomorrow = new Date(iG.getYear(),iG.getMonth()-1,iG.getDay());
    tomorrow.setDate(tomorrow.getDate()+1);
    var curDate = new Date(year,month-1,day);
    var prevDate = new Date(year,month-1,day);
    prevDate.setDate(curDate.getDate()-1);
    var nextDate = new Date(year,month-1,day);
    nextDate.setDate(curDate.getDate()+1);
    var prev = {
        "year" : iG.getYear(prevDate),
        "month" : iG.getMonth(prevDate),
        "day" : iG.getDay(prevDate)
    }
    var cur = {
        "year" : year,
        "month" : month,
        "day" : day
    };
    var next = {
        "year" : iG.getYear(nextDate),
        "month" : iG.getMonth(nextDate),
        "day" : iG.getDay(nextDate)
    }
    var prevShow = prev.month + "-" + prev.day;
    var curShow = cur.month + "-" + cur.day;
    var nextShow = next.month + "-" + next.day;
    switch(prevDate.getTime()){
        case yesterday.getTime() : prevShow = "昨天"; break;
        case today.getTime() :  prevShow = "今天"; break;
        case tomorrow.getTime() : prevShow = "明天"; break;
    }
    switch(curDate.getTime()){
        case yesterday.getTime() : curShow = curShow + "(昨天)"; break;
        case today.getTime() :  curShow = curShow + "(今天)"; break;
        case tomorrow.getTime() : curShow = curShow + "(明天)"; break;
    }
    switch(nextDate.getTime()){
        case yesterday.getTime() : nextShow = "昨天"; break;
        case today.getTime() :  nextShow = "今天"; break;
        case tomorrow.getTime() : nextShow = "明天"; break;
    }  
    $("[prevDate]")
        .attr("date",JSON.stringify(prev))
        .children("[dateShow]").text(prevShow);
    $("[curDate]")
        .attr("date",JSON.stringify(cur))
        .children("[dateShow]").text(curShow)
    $("[nextDate]")
        .attr("date",JSON.stringify(next))
        .children("[dateShow]").text(nextShow);
}
//选择日期
flyList.selectDate = function(){
    var dateString = $(this).attr("date");
	var date= getStorage("dateTime");
    var date = JSON.parse(dateString);
    flyList.setDate(date.year,date.month,date.day);
	setStorage("dateTime",(date.year + "-" + date.month + "-" + date.day));
	uexWindow.evaluatePopoverScript("","content","selecteValue()");
}

//查询接口
function selecteValue(){
//    loadingStart("加载中...", 0);
    $.iBoxWait();
	var urlStr='';
	if(getStorage('uploadType')=="hx"){
		urlStr = flightHXUrl;
	}else{
		urlStr = flightUrl;
	}
	var kfid = getStorage("kfid");
	var param  = {"userID":getStorage("userId"),"dateTime":getStorage("dateTime"),"flightNo":getStorage("flightNo"),"kfid":kfid};
	 $.ajax({
        url :urlStr,
        type : "post",
		data : param,
        dataType:"json", 
        success: function(data){
				 if (data.status==0) {
						$$("dataCont").innerHTML="";
						   var successObj=0;
					if((data.array).length>0){
						  for (var i = (data.array).length - 1; i > -1; i--) {
						  	queryPbd((data.array)[i], function(queryObj){
									if (queryObj == 0) {
									successObj++;
									}
									if(successObj==(data.array).length){
									   data.array=null;
                                        setConter();
									}
								})
							}
						}else{
//							 window.location.reload();
                             setConter(); 
						}
					}else if(data.status==1){
						 $.iBoxMsg(data.desc);
//						 loadingEnd();
//						 uexWindow.alert("提示消息", data.desc, "确定");
					}else{
						 $.iBoxMsg("网络异常，请重试");
//						 loadingEnd();
//						 uexWindow.alert("提示消息", "网络异常，请重试", "确定");
					}
		       },
			   error: function(data){
			   	 $.iBoxMsg("当前网络异常，暂时转为离线模式");
//			   	  loadingStart("当前网络异常，暂时转为离线模式", 2000);
			   }
			})
      }

//打开指定的配备单 
function getId(obj){
	setStorage("HBH",getStorage("HBH1"+obj.id));
	setStorage("pbdh",getStorage("F_PBDH1"+obj.id));
	setStorage("F_PBDBH",obj.id);
	uexWindow.open("FlightJGPPEIDAN", 0, "jgp_FlightJGPPeiDan.html", 2, "", "", 0);
}
String.prototype.replaceAll2Excep = function(s1, s2) {        
    var temp = this;        
    while (temp.indexOf(s1) != -1) {        
        temp = temp.replace(s1, s2);        
    }        
    return temp;        
}  

function loadDbDate(){
	        var mycars=['确认', '取消'];
			uexWindow.confirm("提交提示","是否提交所有待提交的配备单？",mycars);
			uexWindow.cbConfirm = function confirmCallback(opId, dataType, data){
				if (data == 1) {
					return;
				}
				else {
					var len = 0;
					var len1 = 0;
					var gwcArrays = new Array();
					var jjcArrays = new Array();
					$("#dataCont li").each(function(){
						var status = $(this).text();
						if (status == "待提交") {
							len++;
						}
					});
					if(len==0){
//						loadingStart("没有待提交的配备单",2000);
						 $.iBoxMsg("没有待提交的配备单");
					}else{
						$("#dataCont li").each(function(){
						var status = $(this).text();
						if (status == "待提交") {
							var id = $(this).attr("id");
							var pbdh = id.substr(6, id.length);
							selectXdLoad(pbdh, function(jsonData){
									var jsonValue = {
										"userId": getStorage("userId"),
										"F_PBDBH": pbdh,
										"tdcArrays": jsonData,
										"gwcArrays": gwcArrays,
										"jjcArrays": jjcArrays
									};
//									loadingStart("加载中...", 0);
									$.iBoxHide();
									var param  = {"Json":JSON.stringify(jsonValue)};
										$.ajax({
								        url : updateUrl,
								        type : "post",
										data : param,
								        dataType:"json",   
										success: function(data){
											if (data.status == 0) {
												updateXd(pbdh, 2);
												len1++;
												if (len == len1) {
													jsonData=null;
													jsonValue=null;
//													loadingStart("提交成功！！", 2000);
                                                    $.iBoxMsg("提交成功");
						                            setConter();
												}
											}
											else 
												if (data.status == 1) {
													loadingEnd();
													var mycars = ['确认', '取消'];
													uexWindow.confirm("提交失败提示", "是否再次提交？", mycars);
													uexWindow.cbConfirm = function confirmCallback(opId, dataType, data1){
														if (data1 == 0) {
															loadDbDate();
														}
													};
												}
												else {
													$.iBoxMsg("网络异常，请重试");
//													loadingEnd();
//													uexWindow.alert("提示消息", "网络异常，请重试", "确定");
												}
										},
										error: function(data){
											$.iBoxMsg("网络异常，数据已经保存至本地，请稍后提交！");
//											 loadingStart("网络异常，数据已经保存至本地，请稍后提交！", 3000);
										}
									})
							});
						}
					});
					}	
				}
			}
	}
//初始化航班列表页面
function setConter(){
	    $$("dataCont").innerHTML="";
		showPbd(function(jsonArry){
		jsonArry=jsonArry.replaceAll2Excep("\\","");
		jsonArry=jsonArry.replaceAll2Excep("\"[","[");
		jsonArry=jsonArry.replaceAll2Excep("]\"","]");
		var json=JSON.parse(jsonArry);
			 if (json.length > 0) {
			 	for (var i = json.length - 1; i > -1; i--) {
				 	var F_PBDH = json[i].F_PBDH;
					var F_PBDBH = json[i].F_PBDBH;
					setStorage("F_PBDH1"+F_PBDBH,F_PBDH);
					var F_CYRBM = json[i].F_CYRBM;
				 	var F_FJJH = json[i].F_FJJH;
					var HBXX =json[i].HBXX;
				 	var F_CFTJW =HBXX[0].F_CFTJW;
					var F_FJJX =HBXX[0].F_FJJX;
					var F_HX =json[i].F_HX;
					var hx =F_HX.split("-");
					var status = json[i].status;
					var liId="status"+F_PBDBH;
					var color;
				if(status==0){
					status="待操作";
					color="tx-r uinn ulev-1";
				}else if(status==1){
					status="待提交";
					color="tx-r t-red uinn ulev-1";
				}else{
					status="已完成";
					color="tx-r t-lui uinn ulev-1";
					
				}
				 $$("dataCont").innerHTML +='<div class="" onclick="getId(this)" id="'+
				 F_PBDBH+
				'">\
				 <div class="touming1  uc-t1">'+
					'<div class="c-wh">'+
					    '<ul class="uc-t ub t-bla ub-ac lis">'+
					        '<li class="ut-s uinn " id="">配备单号:&nbsp;</li>'+
					        '<li class="ub-f1 cu ut-s uinn t-bla" id="">'+
							F_PBDH+
							'</li>'+
					        '<li id="'+
							liId+
							'" class="'+
							color+
							'">'+
							status+
							'</li><li class="listIcon uinn lis-sw ub-img" id=""></li>'+
					    '</ul>'+
					'</div>'+
			    '</div>'+
			    '<div class="">'+
					'<div class="ub c-gra touming t-bla">'+
					    '<div class=" ub-f1  uc-l1">'+
							'<div class="ub ub-ac ub-pc t-bla">'+
							    '<div class="uinn3 uc-t1">机型</div>'+
							   ' <div class="uinn3 cu t-dgra">'+
							   F_FJJX+
							   '</div>'+
							'</div>'+
					    '</div>'+
					    '<div class="ub-f1 ">'+
							'<div class="ub  ub-ac ub-pc t-bla">'+
							    '<div class="uinn3 uc-t1">机号</div>'+
							    '<div class="uinn3 cu t-dgra">'+
								F_FJJH+
								'</div>'+
							'</div>'+
					    '</div>'+
					    '<div class="ub-f1  uc-r1">'+
							'<div class="ub  ub-ac ub-pc t-bla">'+
							    '<div class="uinn3 uc-t1">桥位</div>'+
							   '<div class="uinn3 cu t-dgra">'+
							   F_CFTJW+
							   '</div>'+
							'</div>'+
					    '</div>'+
					'</div>'+
			    '</div>'+
			    '<div class="ub ubb uinnT  c-blu7 touming " style= "border-bottom:10px solid;border-bottom-color:#FFFFFF;" id="conter2'+
				F_PBDH+
			'"></div></div>';
			if (HBXX.length < 4) {
			 $$("conter2"+F_PBDH).className='ub ubb1 b-wh uinnT  c-blu7 touming t-bla ';
			}else if (HBXX.length == 4) {
			 $$("conter2"+F_PBDH).className='ub ubb1 b-wh uinnT umw35 c-blu7 touming t-bla ';
			}else if(HBXX.length == 5){
			 $$("conter2"+F_PBDH).className='ub ubb1 b-wh uinnT umw36 c-blu7 touming t-bla ';
			}else if(HBXX.length == 6){
			 $$("conter2"+F_PBDH).className='ub ubb1 b-wh uinnT umw37 c-blu7 touming t-bla ';
			}else if(HBXX.length > 6){
			 $$("conter2"+F_PBDH).className='ub ubb1 b-wh uinnT umw38 c-blu7 touming t-bla ';	
			}
			 for (var j = 0; j < HBXX.length+1; j++) {
			 if(j < HBXX.length){
			 hbh = F_CYRBM+HBXX[j].F_HBH;
			 if(j==0){
			 	setStorage("HBH1"+F_PBDBH,hbh);
			 }
			 redId="red"+j+F_PBDBH;
			 $$("conter2"+F_PBDH).innerHTML +='<div class="ub-f1 uinn3 uc-l1 ub ub-ac ulev0 ub-pc">'+
			 hx[j]+
			 '</div>\
			    <div class="ub-f3">\
					<div class="ub ub-ver ub-ac ub-pc">\
					    <div class="ub ub-ac ub-pc  ubb b-wh ulev-1 uc-t1" id='+
						redId+
						'>'+
						hbh+
						'</div>\
					    <div class="uc-b1"></div>\
					</div>\
			    </div>';
				}else{
			 $$("conter2"+F_PBDH).innerHTML +='<div class="ub-f1 uinn3 ulev0 uc-l1 ub ub-ac ub-pc">'+
			  hx[j]+
			 '</div>';
				}
			 }
			 $$("red0"+F_PBDBH).className="ub ub-ac ub-pc ubb b-wh  ulev-1 uc-t1 t-red"
			 } 
			       $.iBoxHide();
		 }else{
		 	 $$("dataCont").innerHTML +="<div class='ub ub-ver  t-bla'>\
			    <div class=' uinn uc-t1'>\
			    	<div class='uba b-gra uinn uc-a tx-c'>很抱歉，未查询到相应的航班</div>\
			    </div></div>";
				$.iBoxHide();
		 }
		 
		})
		   
	}
	
